package com.his.controller;



import com.his.entity.Drug;
import com.his.entity.ResponseResult;
import com.his.service.IDrugService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 药品项目表（包括了重要、中成药、草药） 前端控制器
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/drug")
@Tag(name = "药品管理模块接口")
public class DrugController {

    @Resource
    private IDrugService service;

    @PostMapping("/insert")
    @PreAuthorize("hasAuthority('administer')")
    @Operation(summary = "新增药品", description = "至少需要name，price，dosageid，stock")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "返回201，插入成功"),
            @ApiResponse(responseCode = "400", description = "返回400，插入失败"),
    })
    public ResponseResult insert(@RequestBody Drug drug) {
        return service.myInsert(drug);
    }

    @PostMapping("update")
    @PreAuthorize("hasAuthority('administer')")
    @Operation(summary = "根据id更新科室信息", description = "需要管理员权限，id是必要参数")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "返回201，更新成功"),
            @ApiResponse(responseCode = "400", description = "返回400，更新失败"),
    })
    public ResponseResult update(@RequestBody Drug drug) {
        return service.myUpdate(drug);
    }

    @PostMapping("searchAll")
    @Operation(summary = "获取所有药品信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "返回201，查询成功和所有药品信息"),
            @ApiResponse(responseCode = "400", description = "返回400，查询失败"),
    })
    public ResponseResult searchAll() {
        return service.mySearchAll();
    }

    @PostMapping("fuzzySearch")
    @Operation(summary = "模糊查询药品信息", description = "字符串是参数，注意是Post，参数在body里")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "返回201，模糊查询成功和药品信息"),
            @ApiResponse(responseCode = "400", description = "返回400，模糊查询失败"),
    })
    public ResponseResult fuzzySearch(@RequestBody String str) {
        return service.myFuzzySearch(str);
    }

    @PostMapping("delete")
    @PreAuthorize("hasAuthority('administer')")
    @Operation(summary = "根据id删除药品信息", description = "需要管理员权限，id是必要参数")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "返回201，删除成功"),
            @ApiResponse(responseCode = "400", description = "返回400，删除失败"),
    })
    public ResponseResult delete(@RequestBody Drug drug) {
        return service.myDelete(drug);
    }
}